const express = require('express');
const router = express.Router();
const con = require("../db")
const con2 = require("../db/mysql")
const md5 = require("md5")
/* GET home page. */

// 分页查询
router.get('/', async (req, res) => {
  const { page, limit,industry,state,compary } = req.query;
  let where = "";
  // if (industry) {
  //   where += ` AND customer.industry="${industry}"`
  // }
  // if (state) {
  //   where += `AND customer.state="${state}" `
  // }
  // if (compary) {
  //   where += `AND customer LIKE '%${compary}%'`
  // }
  const result = await con2.query(`
    SELECT * FROM contract WHERE 1 ${where} ORDER BY id DESC LIMIT ${limit} OFFSET ${(page - 1) * limit};
    SELECT COUNT(*) AS total FROM contract
 `);
  res.json({
    msg: "查询成功",
    code: 200,
    data: result[0],
    total: result[1][0].total,

  })
});
router.delete('/:id', (req, res) => {
  const { id } = req.params;
  con.query(`DELETE FROM customer WHERE id=?`, id, (err, result) => {
    if (err) throw err;

    if (result.affectedRows === 1) {
      res.json({
        msg: "删除成功",
        code: 200
      });
    } else {
      res.json({
        msg: "删除失败",
        code: 500
      });
    }
  });
});

router.post('/', async (req, res) => {
  const { body } = req;

  try {
    const result = await con2.query(
      `INSERT INTO contract (
        code, undertake, cid, handler, types, starttime, endtime, status,
        smethods, phone, pay, paytime, invoice, paperfile, server, contract,
        sign, signtime, state, deadline, con_name, money, already, situation,
        digital, agreement, upload
      ) 
      VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
      [
        body.code, body.undertake, body.cid, body.handler, body.types,
        body.starttime, body.endtime, body.status, body.smethods, body.phone,
        body.pay, body.paytime, body.invoice, body.paperfile, body.server,
        body.contract, body.sign, body.signtime, body.state, body.deadline,
        body.con_name, body.money, body.already, body.situation, body.digital,
        body.agreement, body.upload
      ]
    );

    if (result.affectedRows === 1) {
      res.json({
        msg: "添加成功",
        code: 200
      });
    } else {
      res.json({
        msg: "添加失败",
        code: 500
      });
    }
  } catch (error) {
    console.error("添加合同时出错:", error);
    res.status(500).json({
      msg: "添加合同时出错",
      code: 500
    });
  }
});

router.put('/:id', (req, res) => {
  const { id } = req.params;
  const { compary,
    industry,
    province,
    name,
    phone,
    title,
    state,
    mid,
    needs} = req.body;

    con.query(`UPDATE customer SET 
    compary=?,
    industry=?,
    province=?,
    name=?,
    phone=?,
    title=?,
    state=?,
    mid=?,
    needs=? WHERE id=?`, [compary,
      industry,
      province,
      name,
      phone,
      title,
      state,
      mid,
      needs,
      id], (err, result) => {
        if (err) throw err;
        if (result.affectedRows === 1) {
          res.json({
            msg: "更新成功",
            code: 200
          })
        } else {
          res.json({
            msg: "更新失败",
            code: 500
          })
        }
      })
    
  
});
module.exports = router;
